package com.server.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.server.service.ICommonDataService;
import com.server.service.IGradeManagementService;

@Service("GradeManagementService")
public class GradeManagementService implements IGradeManagementService {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private ICommonDataService commonDataService;
	
	/**
	 * 获取所有的评价
	 * @param type
	 * @param id
	 * @return
	 * @author ServerZhang
	 * @date 2016年8月10日
	 */
	@Override
	public List<Map<String, Object>> getEvaList(String type,Integer id){
		String sql = "select * from education.course_score where type='%s' and typeId=%s";
		return jdbcTemplate.queryForList(String.format(sql, type,id));
	}
	 /**
	 * 获取总评分和我的评分
	 * @param type
	 * @param id
	 * @return
	 * @author ServerZhang
	 * @date 2016年8月10日
	 */
	@Override
	public Map<String, Object> getScore(String type,Integer id,String userName){
		Map<String, Object> returnMap = new HashMap<String, Object>();
		String sql = "select * from education.course_score where type='%s' and typeId=%s";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, type,id));
		Iterator<Map<String, Object>> iterator = list.iterator();
		Double acceptScore = 0.0;
		Double qualityScore = 0.0;
		Double myAcceptScore = 0.0;
		Double myQualityScore = 0.0;
		Integer numCount = 0;
		while(iterator.hasNext()){
			numCount ++;
			Map<String, Object> next = iterator.next();
			Object createrObj = next.get("creater");
			Object acceptObj = next.get("accept");
			if (acceptObj != null) {
				Double accept = Double.parseDouble(acceptObj.toString());
				acceptScore += accept;
				if (createrObj != null && !StringUtils.isEmpty(userName) && userName.equals(createrObj.toString())) {
					myAcceptScore = accept;
				}
			}
			Object qualityObj = next.get("quality");
			if (qualityObj != null) {
				Double quality = Double.parseDouble(qualityObj.toString());
				qualityScore += quality;
				if (createrObj != null && !StringUtils.isEmpty(userName) && userName.equals(createrObj.toString())) {
					myQualityScore = quality;
				}
			}
		}
		returnMap.put("acceptScore", acceptScore);
		returnMap.put("qualityScore", qualityScore);
		returnMap.put("numCount", numCount);
		returnMap.put("myAcceptScore", myAcceptScore);
		returnMap.put("myQualityScore", myQualityScore);
		return returnMap;
	}
}
